package cn.yunhe.util;

import cn.yunhe.controller.ExamController;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.*;
import java.util.HashMap;

import java.util.Map;

public class DocUtil {
    public static File createDoc(Map<String,Object> dataMap,Class resourceLoaderClass) throws IOException {

        // 获取模板
        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
        configuration.setClassForTemplateLoading(resourceLoaderClass, "/static/resources/orginTemp");
        Template t = null;
        String name = "temp" + (int) (Math.random() * 1000) + ".doc";
        File file = new File(name);
        try {
            t = configuration.getTemplate("exampaperTemp.xml");
            t.setEncoding("UTF-8");
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(name), "UTF-8"));
            t.process(dataMap, out);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        return file;
    }
}
